package com.jdbc.ID;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.jdbc.core.JdbcTemplate;

import java.util.List;

public class MainID {
    public static void main(String[] args) {
        ApplicationContext ctx = new ClassPathXmlApplicationContext("spring_cfg_JDBC.xml");
        JdbcTemplate jdbcTemplate = (JdbcTemplate) ctx.getBean("jdbcTemplate");
        String sql = "select * from testId where id = 32";
        select(jdbcTemplate, sql);
    }

    // 查找所有
    private static void select(JdbcTemplate jdbcTemplate, String sql) {
        System.out.println("方法 一：");
        List<IDBean> list = jdbcTemplate.query(sql, (resultSet, i) -> {
            IDBean tID = new IDBean();
            tID.setID(resultSet.getInt("id"));
            tID.setUID(resultSet.getString("uID"));
            tID.setUsable(resultSet.getString("usable"));
            return tID;
        });
        System.out.println("listSize = " + list.size());
        for (IDBean bean : list) {
            System.out.print("姓名：" + bean.getID());
            System.out.print("\t性别：" + bean.getUID());
            System.out.println("\t可用：" + bean.getUsable());
        }
    }
}